#include <cv_bridge/cv_bridge.h>
#include <interface/msg/images.hpp>
#include <opencv2/opencv.hpp>
#include <rclcpp/rclcpp.hpp>

class UiEventPlugin : public rclcpp::Node {
public:
    UiEventPlugin(std::string name)
        : Node(std::move(name))
    {
        auto mat = cv::imread("/home/nvidia/workspace/cwy/mem/src/other/ui/ui_event_plugin/image.jpg");
        publisher_ = this->create_publisher<interface::msg::Images>("/sensor/camera/images", 0);

        auto header = std_msgs::msg::Header();
        auto nsTs = std::chrono::system_clock().now().time_since_epoch().count();
        header.stamp.sec = nsTs / 1000000000;
        header.stamp.nanosec = nsTs % 1000000000;
        header.frame_id = "camera1";

        auto msg = cv_bridge::CvImage(header, "bgr8", mat).toImageMsg();

        for (size_t i = 0; i < 7; i++) {
            auto image = *msg;
            image.header.frame_id = "cmaera" + std::to_string(i);
            images.images.emplace_back(std::move(image));
        }

        timer_ = this->create_wall_timer(std::chrono::milliseconds(1000), [=] {
            size_t size = 0;
            for (auto& i : images.images) {
                size += i.data.size();
            }
            std::cout << size / 1024 / 1024 << std::endl;
            publisher_->publish(images);
        });
    }

public:
    interface::msg::Images images;
    std::shared_ptr<rclcpp::Publisher<interface::msg::Images>> publisher_;
    std::shared_ptr<rclcpp::TimerBase> timer_;
};

int main(int argc, char** argv)
{
    rclcpp::init(argc, argv);

    auto nodeSptr = std::make_shared<UiEventPlugin>("UiEventPlugin");

    rclcpp::spin(nodeSptr);
    rclcpp::shutdown();

    return 0;
}